package com.tan.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tan.domain.Blog;
import com.tan.domain.Tag;
import com.tan.domain.User;
import com.tan.domain.response.Result;
import com.tan.domain.response.StatusCode;
import com.tan.domain.vo.TagVO;
import com.tan.mapper.BlogMapper;
import com.tan.mapper.CommentMapper;
import com.tan.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: longshifu
 * @time: 2022/7/13 22:11
 */
@RestController
@RequestMapping("/admin")
public class AdminIndexController {
    @Autowired
    BlogService blogService;
    @Autowired
    BlogMapper blogMapper;
    @Autowired
    CommentService commentService;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    TagService tagService;
    @Autowired
    TypeService typeService;
    @Autowired
    UserService userService;

    //获取博客数量
    @GetMapping("/getBlogCount")
    public Result getBlogCount() {
        int count = blogService.count();
        return new Result(true, StatusCode.OK, "获取博客总数成功", count);
    }

    //获取总阅读量
    @GetMapping("/getViewCount")
    public Result getViewCount() {
        return new Result(true, StatusCode.OK, "获取阅读总数成功", blogMapper.countViews());
    }

    //获取总点赞数
    @GetMapping("/getAppreciateCount")
    public Result getAppreciateCounts() {
        return new Result(true, StatusCode.OK, "获取赞赏总数成功", blogMapper.countAppreciate());
    }

    //获取总评论数
    @GetMapping("/getCommentCount")
    public Result getCommentCount() {
        return new Result(true, StatusCode.OK, "获取评论总数成功", commentService.count());
    }

    //根据月份统计阅读量
    @GetMapping("/getViewCountByMonth")
    public Result getBlogViewsByMonth() {
        return new Result(true, StatusCode.OK, "获取按月份统计阅读总数成功", blogMapper.viewCountByMonth());
    }

    //根据月份统计博客发表数
    @GetMapping("/getBlogCountByMonth")
    public Result getBlogCountByMonth() {
        return new Result(true, StatusCode.OK, "获取按月份统计发表总数成功", blogMapper.blogCountByMonth());
    }

    //根据月份统计评论数
    @GetMapping("/getCommentCountByMonth")
    public Result getCommentCountByMonth() {
        return new Result(true, StatusCode.OK, "获取按月份统计评论总数成功", commentMapper.commentCountByMonth());
    }

    //根据月份统计总点赞数
    @GetMapping("/getAppreciateCountByMonth")
    public Result getAppreciateCountByMonth() {
        return new Result(true, StatusCode.OK, "获取按月份统计评论总数成功", blogMapper.appreciateCountByMonth());
    }

    //后台管理首页获取博客全部标签成功
    @GetMapping("/getFullTagList")
    public Result getFullTagList() {
        List<Tag> tagList = tagService.list();
        // 按标签名字去重
        tagList = tagList.stream().collect(
                Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Tag::getName))), ArrayList::new));
        return new Result(true, StatusCode.OK, "后台管理首页获取博客全部标签成功",tagList);

    }

    //获取博客全部分类
    @GetMapping("/getFullTypeList")
    public Result getFullTypeList() {
        return new Result(true, StatusCode.OK, "获取博客全部分类成功",typeService.list());
    }

    //获取评论列表
    @GetMapping("/getCommentList")
    public Result getCommentList() {
        return new Result(true, StatusCode.OK, "获取评论列表成功",commentService.list());
    }

    //获取用户列表
    @GetMapping("/getUserAreaList")
    public Result getuserAreaList(){
        List<User> list = userService.list();
        return new Result(true,StatusCode.OK,"获取用户列表成功",list);
    }
}
